package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.zjmzxfzhl.modules.app.entity.AppProdUser;
import com.zjmzxfzhl.modules.app.form.AppProdUserVo;
import com.zjmzxfzhl.modules.app.form.AppProjUserVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppProdUser;
import com.zjmzxfzhl.modules.app.mapper.AppProdUserMapper;
import com.zjmzxfzhl.modules.app.service.AppProdUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppProdUserServiceImpl extends BaseServiceImpl<AppProdUserMapper, AppProdUser>
        implements AppProdUserService {
    @Override
    public IPage<AppProdUserVo> list(IPage<AppProdUserVo> page, AppProdUserVo appProdUserVo) {
        return page.setRecords(baseMapper.list(page, appProdUserVo));
    }
    @Override
    public List<AppProdUserVo> getList(IPage<AppProdUserVo> page, AppProdUserVo appProdUserVo) {
        return baseMapper.list(page, appProdUserVo);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProdUser(AppProdUser appProdUser) {
        Map<SFunction<AppProdUser,?>,Object> params=new HashMap<>();
        if(appProdUser.getProdId()!=null&&!"".equals(appProdUser.getProdId())){
            params.put(AppProdUser::getProdId,appProdUser.getProdId());
        }
        if(appProdUser.getUserId()!=null&&!"".equals(appProdUser.getUserId())){
            params.put(AppProdUser::getUserId,appProdUser.getUserId());
        }
        Integer total=this.count(new LambdaQueryWrapper<AppProdUser>().allEq(params));
        if(total>0){
            return false;
        }
        appProdUser.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Boolean result=this.save(appProdUser);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProdUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppProdUser appProdUser) {
        Map<String, Object> columnMap=new HashMap<>();
        if (appProdUser.getProdId() == null && appProdUser.getUserId() == null) {
            return false;
        }
        if(appProdUser.getProdId()!=null){
            columnMap.put("PROD_ID",appProdUser.getProdId());
        }
        if(appProdUser.getUserId()!=null){
            columnMap.put("USER_ID",appProdUser.getUserId());
        }
        Boolean result=this.removeByMap(columnMap);
        return result;
    }
}
